package com.example.springsecurity.config;

import com.example.springsecurity.authentication.JwtAuthenticationFilter;
import com.example.springsecurity.authentication.SmsAuthenticationConfigurer;
import com.example.springsecurity.handler.MyAuthenticationEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * {@code @Author:} anwenbiao
 * {@code @Description:} 安全配置类
 * {@code @DateTime:} 2023/2/421:41
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {
    

    @Resource
    private MyAuthenticationEntryPoint authenticationEntryPoint;

    @Resource
    private SmsAuthenticationConfigurer smsAuthenticationConfigurer;
    
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity, 
                                                   AuthenticationSuccessHandler authenticationSuccessHandler,
                                                   AuthenticationFailureHandler authenticationFailureHandler) throws Exception {
        
        httpSecurity.formLogin()
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                // 配置session的创建策略
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                .and()
                .authorizeHttpRequests().anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .and()
                .csrf().disable();
        httpSecurity.addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
        httpSecurity.apply(smsAuthenticationConfigurer);
        return httpSecurity.build();
        
    }

//    @Bean
//    public WebSecurityCustomizer webSecurityCustomizer(WebSecurity web) {
//        return (web) -> web.ignoring().antMatchers("/resources/**");
//    }
    
    @Bean
    public PasswordEncoder passwordEncoder(){
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

//    @Bean
//    public AuthenticationManager authenticationManager(){
//        new AuthenticationManagerBuilder()
//    }
}
